package com.aj.activity;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.aj.activity.adapter.HomeFragmentPagerAdapter;
import com.aj.activity.fragment.ChildPhotoFragment;
import com.aj.activity.fragment.PrayLetterFragment;
import com.aj.databases.entry.FamilySpace;
import com.aj.utils.Contants;
import com.aj.utils.PortraitLoaderUtil;

public class ChildSpaceActivity extends BaseFragmentActivity {

	private RadioGroup photoview_tools;
	private RadioButton child_photo_radio;
	private RadioButton pray_letter_radio;
	private ViewPager photo_body;
	private ImageView son_head_img;
	private TextView son_name_text;
	private TextView son_ajnumber_text;
	private ArrayList<Fragment> fragmentList;
	private FamilySpace familySpace;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_mychildspace);
		initView();
		initViewPager();
	}

	private void initView() {
		familySpace = (FamilySpace) getIntent().getSerializableExtra(
				Contants.USERINFOFLAG_STRING);
		photoview_tools = (RadioGroup) findViewById(R.id.photoview_tools);
		child_photo_radio = (RadioButton) findViewById(R.id.child_photo_radio);
		pray_letter_radio = (RadioButton) findViewById(R.id.pray_letter_radio);
		photo_body = (ViewPager) findViewById(R.id.photo_body);
		son_head_img = (ImageView) findViewById(R.id.son_head_img);
		son_name_text = (TextView) findViewById(R.id.son_name_text);
		son_ajnumber_text = (TextView) findViewById(R.id.son_ajnumber_text);
		if (familySpace.getNickName() != null) {
			son_name_text.setText(familySpace.getNickName());
		} else {
			son_name_text.setText("");
		}
		son_ajnumber_text.setText(familySpace.getAjNo());
		PortraitLoaderUtil.load(ChildSpaceActivity.this, son_head_img, null,
				familySpace.getPhoto(), R.drawable.head_icon,
				R.drawable.head_icon);

		setActionBarTitle(R.string.child_album_title_text);
	}

	private void initViewPager() {
		ChildPhotoFragment childPhotoFragment = new ChildPhotoFragment();
		PrayLetterFragment prayLetterFragment = new PrayLetterFragment();
		fragmentList = new ArrayList<Fragment>();
		fragmentList.add(childPhotoFragment);
		fragmentList.add(prayLetterFragment);
		// ViewPager设置适配器
		photo_body.setAdapter(new HomeFragmentPagerAdapter(
				getSupportFragmentManager(), fragmentList));
		// ViewPager显示第一个Fragment
		photo_body.setCurrentItem(0);
		// ViewPager页面切换监听
		photo_body.setOnPageChangeListener(new myOnPageChangeListener());
		photoview_tools.setOnCheckedChangeListener(new CheckChangeListener());
	}

	/**
	 * RadioButton切换Fragment
	 */
	private class CheckChangeListener implements
			RadioGroup.OnCheckedChangeListener {

		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			switch (checkedId) {
			case R.id.child_photo_radio:
				// ViewPager显示第一个Fragment且关闭页面切换动画效果
				photo_body.setCurrentItem(0, false);
				break;
			case R.id.pray_letter_radio:
				photo_body.setCurrentItem(1, false);
				break;
			}
		}
	}

	/**
	 * ViewPager切换Fragment,RadioGroup做相应变化
	 */
	private class myOnPageChangeListener implements
			ViewPager.OnPageChangeListener {

		@Override
		public void onPageScrolled(int position, float positionOffset,
				int positionOffsetPixels) {

		}

		@Override
		public void onPageSelected(int position) {
			switch (position) {
			case 0:
				photoview_tools.check(R.id.child_photo_radio);
				break;
			case 1:
				photoview_tools.check(R.id.pray_letter_radio);
				break;
			}
		}

		@Override
		public void onPageScrollStateChanged(int state) {

		}
	}
}
